PostgreSQL 源码最佳实践
1 编译postgresql 软件包并安装到指定目录
1.1 Lastest
#postgres>
cd /soft/postgresql/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-ssl=openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-zstd --with-lz4 --with-tcl --enable-depend --enable-cassert --enable-debug --enable-dtrace CFLAGS="-ggdb -O0"
make -j 8 && make install
1.2 PG17
#postgres>
cd /soft/
cd /soft/postgresql*/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-ssl=openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-zstd --with-lz4 --with-tcl
make -j 8 && make install
1.3 PG16
#postgres>
cd /soft/
tar -zxf postgresql-*.tar.gz
cd /soft/postgresql-*/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-ssl=openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-zstd --with-lz4 --with-tcl
make -j 8 && make install
1.4 PG15
#postgres>
cd /soft/
tar -zxf postgresql-*.tar.gz
cd /soft/postgresql-*/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-ssl=openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-zstd --with-lz4 --with-tcl
make -j 8 && make install
Note
新加 --with-zstd
--with-lz4
两个选项
1.5 PG12
#postgres>
cd /soft/
tar -zxf postgresql-*.tar.gz
cd /soft/postgresql-*/
./configure --prefix=/usr/local/pgsql --with-llvm --with-systemd --with-openssl --enable-debug --with-icu --enable-nls --with-zlib --with-libxml --with-perl --with-ldap --with-python --with-libxslt --with-uuid=e2fs --with-pam --with-gssapi --with-tcl
make -j 8 && make install
Configure常用配置选项:
选项 | 说明 |
---|---|
prefix | 指定软件的安装路径 |
with-openssl | 对openssl进行扩展支持 |
with-python | 对python进行扩展支持 |
with-perl | 对perl进行扩展支持 |
with-libxml | 对xml进行扩展支持 |
--with-pgport | 数据库端口号 |
--with-tcl | 对C 语言扩展支持 |
--with-pam | 使用PAM(可插入身份验证模块)支持构建。 |
--with-libxml | 对 libxml2 扩展 |
--with-blocksize | 数据库块大小。 |
--with-llvm | 支持基于 LLVM 的 JIT 编译进行构建。 |
--with-ssl=openssl | 等同于 with-openssl |
--with-systemd | 使用linux 服务器管理 |
--with-zlib | |
--enable-nls | |
--with-icu | |
--enable-debug | |
--with-libxml | |
--with-uuid=e2fs |
--with-blocksize
- 如果数据库需要经常做插入的操作,数据量增长非常快,尽量把此参数设大一点;
- 经常做小数据查询、更新且内存不是非常大的时候可以设小一点,默认8K即可。
- 生产环境不要加--enable-dtrace --enable-debug。
2 构建文档
#postgres>
make install-docs
3 编译第三方插件并安装
#postgres>
#包括第三方插件全部编译
cd /soft/postgresql*/contrib
#这个需要使用普通用户执行,可选,耗时较长
make -j 8 && make install
4 创建数据库集簇
4.1 创建目录
#postgres>
mkdir -p /usr/local/pgsql/data
4.2 初始化数据库集簇
#postgres>
initdb -D $PGDATA -W --data-checksums -A scram-sha-256 -E UTF-8
Warning
- 要想初始化集簇为英文,需要设置
LANG=en_US.UTF-8
。 - 更多关于Postgersql 能够初始化的字符集请见 PostgreSQL: Documentation: 15: 24.3. Character Set Support
- 复制时需要 data-cecksums 支持块校验。
postgres
用户的密码为postgres
4.3 启动数据库集簇
#postgres>
pg_ctl start -D $PGDATA
4.4 创建新的数据库
postgres>
createdb testdb
4.5 登录数据库
#postgres>
psql testdb